<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:xs="http://www.w3.org/2001/XMLSchema"
	xmlns:f="livcos.org/ns/xslt/function"
	exclude-result-prefixes="f xs">
	
	<xsl:import href="ide_dummy.xsl"/>
	
	<!-- standard extension functions -->
	
	<xsl:function name="f:doc">
		<xsl:param name="uri"/>
		<xsl:if test="doc-available($uri)">
			<xsl:copy-of select="doc($uri)"/>
		</xsl:if>
	</xsl:function>
	
	<xsl:function name="f:eval" exclude-result-prefixes="#all">
		<xsl:param name="context" as="node()"/>
		<xsl:param name="xPath" as="xs:string"/>
		<xsl:param name="nsContext" as="element()"/>
		<!-- build the context for the eval extension function -->
		<xsl:for-each select="$context">
			<xsl:sequence select="f:eval($xPath, $nsContext)"/>
		</xsl:for-each>
	</xsl:function>
	
	<xsl:function name="f:caller-uri" as="xs:string">
		<xsl:param name="baseUri"/>
		<xsl:param name="uri"/>
		<xsl:value-of select="f:caller-uri(concat($baseUri, if (starts-with($uri, '/')) then '' else '/', $uri))"/>
	</xsl:function>
	
	<xsl:function name="f:isValidUrl" as="xs:boolean">
		<xsl:param name="str"/>
		<xsl:value-of select="boolean(f:uri($str)/path)"/>
	</xsl:function>
	
	<xsl:function name="f:format-duration" as="xs:string">
		<xsl:param name="duration" as="xs:duration"/>
		<xsl:variable name="v">
			<xsl:variable name="y" select="years-from-duration($duration)"/>
			<xsl:if test="$y != 0"><xsl:value-of select="$y"/>years </xsl:if>
			<xsl:variable name="M" select="months-from-duration($duration)"/>
			<xsl:if test="$M != 0"><xsl:value-of select="$M"/>months </xsl:if>
			<xsl:variable name="d" select="days-from-duration($duration)"/>
			<xsl:if test="$d != 0"><xsl:value-of select="$d"/>days </xsl:if>
			<xsl:variable name="h" select="hours-from-duration($duration)"/>
			<xsl:if test="$h != 0"><xsl:value-of select="$h"/>h </xsl:if>
			<xsl:variable name="m" select="minutes-from-duration($duration)"/>
			<xsl:if test="$m != 0"><xsl:value-of select="$m"/>min </xsl:if>
			<xsl:variable name="s" select="round(seconds-from-duration($duration))"/>
			<xsl:if test="$s != 0"><xsl:value-of select="$s"/>s </xsl:if>
		</xsl:variable>
		<xsl:value-of select="$v"/>
	</xsl:function>
	
	<xsl:function name="f:total-seconds">
		<xsl:param name="duration" as="xs:duration"/>
		<xsl:variable name="d" select="xs:dayTimeDuration($duration)"/>
		<xsl:variable name="v1" select="minutes-from-duration($d) * 60 + seconds-from-duration($d)"/>
		<xsl:variable name="v2" select="hours-from-duration($d) * 3600 + $v1"/>
		<xsl:value-of	select="days-from-duration($d) * 24 * 3600 + $v2"/>
	</xsl:function>
	
	<xsl:function name="f:cut-start" as="xs:string">
		<xsl:param name="str"/>
		<xsl:param name="subStr"/>
		<xsl:value-of	select="if (starts-with($str, $subStr)) then substring-after($str, $subStr) else $str"/>
	</xsl:function>
	
	<xsl:function name="f:cut-end" as="xs:string">
		<xsl:param name="str"/>
		<xsl:param name="subStr"/>
		<xsl:value-of	
			select="if (ends-with($str, $subStr)) then substring($str, 1, string-length($str) - string-length($subStr)) else $str"/>
	</xsl:function>
	
	<xsl:function name="f:prefix" as="xs:string">
		<xsl:param name="prefix"/>
		<xsl:param name="str"/>
		<xsl:value-of select="concat($prefix, f:cut-start($str, $prefix))"/>
	</xsl:function>
	
	<xsl:function name="f:suffix" as="xs:string">
		<xsl:param name="str"/>
		<xsl:param name="suffix"/>
		<xsl:value-of select="concat(f:cut-end($str, $suffix), $suffix)"/>
	</xsl:function>
	
	<xsl:function name="f:strip-before" as="xs:string">
		<xsl:param name="str"/>
		<xsl:param name="pattern"/>
		<xsl:value-of	select="if (contains($str, $pattern)) then substring-after($str, $pattern) else $str"/>
	</xsl:function>
	
	<xsl:function name="f:strip-after" as="xs:string">
		<xsl:param name="str"/>
		<xsl:param name="pattern"/>
		<xsl:value-of	select="if (contains($str, $pattern)) then substring-before($str, $pattern) else $str"/>
	</xsl:function>
	
	<xsl:function name="f:normalize-line-ends" as="xs:string">
		<xsl:param name="str"/>
		<xsl:value-of select="replace(string-join($str, ''), '\r\n?', '&#10;')"/>
	</xsl:function>
	
</xsl:stylesheet>
